<template>
    <view class="bg-gradual-blue ">
        <view class="cu-bar" style="box-sizing: unset">
            <view class="action" @click="handleChooseLocation">
                <text v-if="userStore.userInfo" class="text-cut" style="max-width: 60px">{{
                    userStore.userInfo.city
                    }}
                </text>
                <text class="cuIcon-triangledownfill"></text>
            </view>
            <view class="search-form round" style="width: 50vw" @click="toSearch">
                <text class="cuIcon-search"></text>
                <input focus :disabled="disable" v-model="curValue" type="text" placeholder="搜医院、医生、护士"
                       @confirm="handleSearch"/>
            </view>
            <view v-if="curValue" class="action" @tap="clearSearch">
                <view class="cuIcon-close"></view>
            </view>
        </view>
    </view>
</template>

<script setup lang="ts">

import {computed, defineEmits, defineProps} from "vue";
import {useUserStore} from "@/store";
import api from "@/api";

const userStore = useUserStore()

const props = defineProps({
    disable: {
        type: Boolean,
        default: false
    },
    value: {
        type: String,
        default: '',
    }
})
const emits = defineEmits(['update:value', 'handleSearch', 'clearSearch'])

const handleSearch = () => {
    emits('handleSearch')
}

const clearSearch = () => {
    emits('clearSearch')
    curValue.value = ''
}

const handleChooseLocation = () => {
    uni.chooseLocation({
        success: function (res) {
            const latitude = res.latitude
            const longitude = res.longitude;
            api.tencent.location(
                latitude,
                longitude
            ).then(re => {
                if (re.data.status === 0) {
                    console.log(re.data.result)
                    userStore.userInfo.city = re.data.result.address_component.city.replace("市", "")
                    userStore.userInfo.province = re.data.result.address_component.city.replace("省", "")
                    console.log(userStore.userInfo)
                } else {
                    console.log("获取信息失败，请重试！")
                }
            })
        }
    });

}

const curValue = computed({
    get: () => {
        return props.value
    },
    set(val) {
        emits('update:value', val)
    }
})

const toSearch = () => {
    if (props.disable) {
        uni.navigateTo({
            url: '/pages/common/search/search'
        })
    }
}

</script>

<style scoped>

</style>